<template>
    <div>
        <div class="img"><img :src="Url+img"/></div>
        <div class="red_ft">
            <div>
                <p>长按识别上方二维码</p>
                <p>跳转后识别关注公众号</p>
                <p>即可接受我的推送</p>
            </div>
        </div>
        <!--这是扫码进来的页面-->
    </div>
</template>

<script>
    export default {
        name: "",
        data(){
            return {
                userid:"",
                img:""
            }
        },
        mounted(){
            this.userid=this.$route.params.id;
            this.getImg();
        },
        methods:{
            getImg(){
                var that=this;

                if(!localStorage.getItem("token")){
                    this.onloads();
                    return false;
                }

                that.request1.post1({
                    url: that.Url + "api/user/index/guanzhugzh",
                    params: {   focusid:that.userid},
                    success: function (res) {
                        console.log(res)
                        if (res.data.code == 1) {
                            that.img=res.data.data.gzherweima
                        } else {
                            alert(res.data.msg)
                        }
                    },
                    fail: function (error) {
                        alert(error.data.msg)
                    }
                })
            },
            onloads(){
                var that=this;
                var ua = navigator.userAgent.toLowerCase();//获取判断用的对象

                if (ua.match(/MicroMessenger/i) == "micromessenger") {
                    console.log("wx打开");
                    this.getCode();
                }else {
                    console.log("h5打开");
                    that.$router.push("/log/login")
                }
            },
            getCode () {
                var that=this;
                var local = window.location.href;// 获取页面url
                that.code = that.getUrlCode().code; // 截取code
                if (that.code == null || that.code === '') { // 如果没有code，则去请求
                    that.$axios.post(that.Url+'api/index/getAppid',{
                        params:{},
                    }).then(function(res){
//                  console.log(res.data.data)
                        if(res.data.code==1){
                            let ress=res.data.data;
                            let appid = ress.appid;
                            console.log("appid:"+appid)
                            window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
                        }
                    }).catch(function (error) {
                        console.log(error)
                    });
                } else {
                    that.login()
                }
                that.$axios.post(that.Url+'api/user.index/third',{code:that.code}
                ).then(function(res){
                    console.log(res);
                    if(res.data.code==1){
                        localStorage.setItem('token', res.data.data.userinfo.token);
                        that.getImg();
                    }
                }).catch(function (error) {
                    console.log(error)
                });
            },
            getUrlCode() { // 截取url中的code方法
                var url = location.search;
                this.winUrl = url;
                var theRequest = new Object();
                if (url.indexOf("?") != -1) {
                    var str = url.substr(1);
                    var strs = str.split("&");
                    for(var i = 0; i < strs.length; i ++) {
                        theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
                    }
                }
                return theRequest
            },
            login(){
                var that=this;
                that.$axios.post(that.Url+'api/user.index/third',{code:that.code}
                ).then(function(res){
                    console.log(res);
                    if(res.data.code==1){
                        localStorage.setItem('token', res.data.data.userinfo.token);
                        that.getImg();
                    }
                }).catch(function (error) {
                    console.log(error)
                });
            },
        }
    }
</script>

<style scoped>
    .img, .img img{
        width: 100%;
    }
    .red_ft{
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        line-height: 24px;
        font-size: 14px;
        background: #FED9E1;
        font-weight: 700;
        margin: 20px auto 0;
        width: 80%;
        border-radius: 8px;
        padding: 10px 5%;
    }
</style>